package com.gitee.form_token.service;

import com.gitee.form_token.common.*;
import com.gitee.form_token.token.CheckWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class MyLogoutService {

    @Autowired
    private RedisUtil redisUtil;

    public Result logout(HttpServletRequest request) {

        // Token校验
        String subject = CheckWebToken.getSubject(request);
        String redisKey = "user:" + subject;

        redisUtil.del(redisKey);

        // 清除上下文
        SecurityContextHolder.clearContext();

        return Result.fail("登出成功");
    }

}
